package com.biostime.qdingding.modules.qd.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.biostime.qdingding.modules.qd.entity.Album;
import com.biostime.qdingding.modules.sys.entity.User;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.api.dto.CourseAlbumDto;
import com.biostime.qdingding.modules.qd.api.dto.UploadResultDto;
import com.biostime.qdingding.modules.qd.dao.AlbumDao;;

/**
 * 相册Service
 * @author king
 * @version 2015-11-20
 */
@Service
@Transactional(readOnly = true)
public class AlbumService extends CrudService<AlbumDao, Album> {
	
	
	@Autowired 
	private AlbumDao albumDao;

	@Override
	public List<Album> findList(Album entity) {
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		
		return super.findList(entity);
	}
	
	public void deleteAll(Album ca) {
		dao.deleteAll(ca);
	}
	
//	 <!-- ————————————————————————————楚河汉界，谁敢动我的，打死! ————————————————————————————————————-->	
	
	public int apiInsert(Album album){
		return albumDao.apiInsert(album);
	}
	
    /**
     * 批量上传图片
     * @param map
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @SuppressWarnings({ "unused", "rawtypes", "unchecked", "deprecation" })
	public int apiInsertAlbums(Map<String,Object> map) throws JsonParseException, JsonMappingException, IOException{
		List<UploadResultDto> uploadList = null;
		String picRes = (String) map.get("picRes");	
		String id = (String) map.get("objectId");
		if(picRes != null){
			picRes=picRes.replaceAll(".*\\[", "[").replaceAll("\\].*", "]");
			ObjectMapper mapper = new ObjectMapper();
			List list=mapper.readValue(picRes, List.class);
			JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, UploadResultDto.class);
			uploadList=(List<UploadResultDto>)mapper.readValue(picRes, javaType);
		}	
		//批量插入图片
		if(uploadList != null){
			for(int ii=0 ;ii<uploadList.size();ii++){
				Album album =new Album();
				album.setCreateBy(new User("0")); 
				album.setUpdateBy(new User("0")); 
				album.setCreateDate(new Date());
				album.setUpdateDate(new Date()); 
				album.setDelFlag("0"); 						
				album.setOrginPath(uploadList.get(ii).getPath());  
				album.setThumbPath(uploadList.get(ii).getThumb()); 
				album.setAlbumSize((int)uploadList.get(ii).getSize());
				album.setFileName(uploadList.get(ii).getName());
				album.setObjectId(Integer.parseInt(id)); 
				album.setUploadTime(new Date());
				album.setCategory(uploadList.get(ii).getType());
				albumDao.apiInsert(album);	
			}				
		}
		return 0;	 			
	}
    
    public List<Album> apiGet(String category,String objectId){
    	return albumDao.apiGet(category, objectId);
    }
    
    public void apiDelByObject(Album album){
    	dao.apiDelByObject(album);
    }
    
    public int apiUpdateHeadImg(Album album){
    	return dao.apiUpdateHeadImg(album);
    }
    
    public List<CourseAlbumDto> apiCourseAlbum(String centerId,String studentId){
    	return dao.apiCourseAlbum(centerId,studentId);    	
    }
    
    public List<HashMap<String,Object>> apiGetScollImages(String centerId , String categoryId){
    	return dao.apiGetScollImages(centerId,categoryId);
    }
}
